package com.yanqu.road.dao.impl.activity.hongxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hongxian.HongXianMemory;
import com.yanqu.road.entity.activity.hongxian.HongXianReward;
import com.yanqu.road.entity.activity.hongxian.HongXianUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class HongXianUserDataDaoImpl extends TempDataObjectDao<HongXianUserData> {

    public static String getHongXianUserDataSql = "select * from `t_u_hong_xian_user_data` where `activityId`=? and `userId`=?";
    public HongXianUserData getHongXianUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getHongXianUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateHongXianUserDataSql = "update `t_u_hong_xian_user_data` set selectReward=?,poolDrawProcess=?,score=?,drawTimes=?,perception=?,clientAnim=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(HongXianUserData hongXianUserData) {
        hongXianUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hongXianUserData.getSelectReward())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hongXianUserData.getPoolDrawProcess())));
        para.put(i++, new DbParameter(Types.BIGINT, hongXianUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, hongXianUserData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, hongXianUserData.getPerception()));
        para.put(i++, new DbParameter(Types.VARCHAR, hongXianUserData.getClientAnim()));
        para.put(i++, new DbParameter(Types.INTEGER, hongXianUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, hongXianUserData.getUserId()));
        if (getDb().executeNoneQuery(updateHongXianUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHongXianUserDataSql = "insert into `t_u_hong_xian_user_data`(activityId,userId,selectReward,poolDrawProcess,score,drawTimes,perception,clientAnim) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HongXianUserData hongXianUserData) {
        hongXianUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hongXianUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, hongXianUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hongXianUserData.getSelectReward())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hongXianUserData.getPoolDrawProcess())));
        para.put(i++, new DbParameter(Types.BIGINT, hongXianUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, hongXianUserData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, hongXianUserData.getPerception()));
        para.put(i++, new DbParameter(Types.VARCHAR, hongXianUserData.getClientAnim()));
        if (getDb().executeNoneQuery(insertHongXianUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HongXianUserData parseData(ResultSet rs) throws SQLException {
        HongXianUserData hongXianUserData = new HongXianUserData();
        hongXianUserData.setActivityId(rs.getInt("activityId"));
        hongXianUserData.setUserId(rs.getLong("userId"));
        hongXianUserData.setSelectReward(JSON.parseObject(rs.getString("selectReward"), new TypeReference<Map<Integer, HongXianReward>>() {}));
        hongXianUserData.setPoolDrawProcess(JSON.parseObject(rs.getString("poolDrawProcess"), new TypeReference<Map<Integer, HongXianMemory>>() {}));
        hongXianUserData.setScore(rs.getLong("score"));
        hongXianUserData.setDrawTimes(rs.getInt("drawTimes"));
        hongXianUserData.setPerception(rs.getInt("perception"));
        String clientAnim = rs.getString("clientAnim");
        if(!StringUtils.isNullOrEmpty(clientAnim)){
            hongXianUserData.setClientAnim(clientAnim);
        }
        return hongXianUserData;
    }
}
